package com.cashmama.market.api.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.domain.vo.risk.RiskUserBlackListDTO;
import com.cashmama.market.api.infras.config.ApolloConfigUtil;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.HttpUtil;
import com.fqgj.xjd.user.client.UserInfoService;
import com.fqgj.xjd.user.client.UserPanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2019-12-23 17:13
 **/
@Service
public class RiskService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RiskService.class);
    private static final String RISK_QUERY_BLACKLIST_API = "/black/queryBlack";
    @Autowired
    UserPanService userPanService;

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    ApolloConfigUtil apolloConfigUtil;

    @Autowired
    RedisClient redisClient;

    /**
     * 是否命中黑名单.
     */
    public boolean hitBlacklist() {
        String mobile = RequestLocalInfo.getCurrentUser().getMobile();
        RiskUserBlackListDTO RiskUserBlackListDTO = new RiskUserBlackListDTO();
        RiskUserBlackListDTO.setMobile(mobile);
        List<RiskUserBlackListDTO> RiskUserBlackListDTOList = queryBlackList(RiskUserBlackListDTO);
        return CollectionUtils.isNotEmpty(RiskUserBlackListDTOList);
    }


    private List<RiskUserBlackListDTO> queryBlackList(RiskUserBlackListDTO request) {
        String riskQueryBlackListApi = apolloConfigUtil.getRiskBaseUrl() + RISK_QUERY_BLACKLIST_API;

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");

        String requestBody = JSON.toJSONString(request);
        LOGGER.info("queryBlackList-查询黑名单-requestBody[{}]", requestBody);

        String response = HttpUtil.postPageBody(riskQueryBlackListApi, headers, requestBody);
        LOGGER.info("queryBlackList-查询黑名单-riskQueryBlackListApi[{}],response:[{}]", riskQueryBlackListApi, JSON.toJSONString(response));

        JSONObject responseObject = JSON.parseObject(response);
        List<RiskUserBlackListDTO> resultList = new LinkedList<>();

        if (responseObject != null && Objects.equals(responseObject.getInteger("code"), 0)) {

            JSONArray resultData = responseObject.getJSONArray("data");
            if (resultData == null || resultData.size() == 0) {
                return resultList;
            }

            for (int i = 0; i < resultData.size(); i++) {

                JSONObject jsonObject = resultData.getJSONObject(i);

                RiskUserBlackListDTO result = new RiskUserBlackListDTO();
                result.setAadhaarNo(jsonObject.getString("aadhaarNo"));
                result.setPassport(jsonObject.getString("passport"));
                result.setDriverLicense(jsonObject.getString("driverLicense"));
                result.setVetorId(jsonObject.getString("vetorId"));

                result.setMobile(jsonObject.getString("mobile"));
                result.setUserName(jsonObject.getString("userName"));
                result.setReason(jsonObject.getString("reason"));

                resultList.add(result);
            }
            return resultList;
        } else {
            return null;
        }
    }
}
